package com.glorysoft.knowlespda.ui.setting

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.base.MyApp
import com.glorysoft.knowlespda.databinding.ActivitySettingBinding
import com.glorysoft.knowlespda.entity.ui.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_InitUtils
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import kotlinx.android.synthetic.main.activity_setting.*

/**
 * FileName: SettingActivity
 * Author: XJH
 * Date: 2022/1/11 18:00
 * Description:
 * History:
 */
class SettingActivity : BaseActivity<SettingViewModel, ActivitySettingBinding>(),
    View.OnClickListener {

    private var mSeverDialog: FreeUI_GeneralFragmentDialog? = null
    private var tiph = ""


    override fun layoutId(): Int {
        return R.layout.activity_setting
    }

    override fun initVariableId(): Int {
        return BR.settingViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(this.getString(R.string.system_setup))
        tiph = this.getString(R.string.saved_successfully)
        viewModel.initVersion(this)
        rlIP.setOnClickListener(this)
        rlUpdateIp.setOnClickListener(this)
        tvTime.setOnClickListener(this)
    }

    override fun initData() {

    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.rlIP ->{
                getCustomDialog(object : onConfirmClick{
                    override fun setOnConfirmClick(s: String) {

                        viewModel.constant.value!!.REQUEST_URL_DEBUG = s
                        viewModel.defUI.toastEvent.value = ToastMessageBean(tiph, true)
                        FreeApi_SharePreferencesUtils.setSharePre("KNOWLES_FILE", "REQUEST_URL_DEBUG", s, MyApp.app)
                    }

                },Constant.REQUEST_URL_DEBUG).show()
            }
            R.id.rlUpdateIp ->{
                getCustomDialog(object : onConfirmClick{
                    override fun setOnConfirmClick(s: String) {

                        viewModel.constant.value!!.FILE_URL = s

                        viewModel.defUI.toastEvent.value = ToastMessageBean(tiph, true)
                        FreeApi_SharePreferencesUtils.setSharePre("KNOWLES_FILE", "FILE_URL", s, MyApp.app)
                    }

                },Constant.FILE_URL).show()
            }
            R.id.tvTime -> {
                getCustomDialog(object : onConfirmClick{
                    override fun setOnConfirmClick(s: String) {
                        try {
                            val toInt = s.toInt()
                            viewModel.constant.value!!.RESPONSE_TIME = toInt

                            viewModel.defUI.toastEvent.value = ToastMessageBean(tiph, true)
                            FreeApi_SharePreferencesUtils.setSharePre("KNOWLES_FILE", "RESPONSE_TIME", s, MyApp.app)
                            FreeUI_InitUtils.init(applicationContext, MyApp.getInstance(), true, toInt * 1000L, 0)
                            tvTime.setText(s)
                            mSeverDialog?.dismiss()
                        }catch (e : Exception){}
                    }

                },Constant.RESPONSE_TIME.toString()).show()
            }
        }
    }

    private fun getCustomDialog(listener : onConfirmClick,address : String): MaterialDialog {
        val dialog =  MaterialDialog(this)
            .cancelable(false)
            .cornerRadius(12f)
            .customView(R.layout.dialog_scan_confirm_cancel, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val ivClose: ImageView = contentView.findViewById(R.id.ivClose)
        val edit: EditText = contentView.findViewById(R.id.edit)
        val tvConfirm: TextView = contentView.findViewById(R.id.tvConfirm)
        val tvCancel: TextView = contentView.findViewById(R.id.tvCancel)
        edit.setText(address)

        ivClose.setOnClickListener {
            dialog.dismiss()
        }

        tvCancel.setOnClickListener {
            dialog.dismiss()
        }


        tvConfirm.setOnClickListener {
            if (edit.text.toString().isNotEmpty()){
                listener.setOnConfirmClick(edit.text.toString())
                dialog.dismiss()
            }else{
                viewModel.defUI.toastEvent.value = ToastMessageBean(this.getString(R.string.please_enter_value),false)
            }

        }

        return dialog
    }

    interface onConfirmClick{
        fun setOnConfirmClick(s : String)
    }
}